package com.heliang.dataaccess.dao.impl;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.exception.DbException;
import android.content.Context;

import com.heliang.dataaccess.bean.InfoCatBean;
import com.heliang.dataaccess.dao.BaseDao;
import com.heliang.dataaccess.dao.InfoCatDao;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.handle.ExceptionHandle;

/**
 * @ClassName: InfoCatDaoImpl.java
 * @Description: 资讯类别相关操作
 * @author heliang
 * @version V1.0
 * @Date 2013-9-3 上午7:02:52
 */
public class InfoCatDaoImpl extends BaseDao implements InfoCatDao {

    public InfoCatDaoImpl(Context context) {
        super(context);
    }

    /**
     * @Description: 批量新增资讯类别
     * @param: @param infoCatBeans
     * @param: @return
     * @return: boolean
     * @throws
     * @author heliang
     * @Date 2013-9-9 上午7:17:19
     */
    public boolean saveInfoCats(List<InfoCatBean> infoCatBeans) throws DaoException{
        try {
            for (InfoCatBean infoCatBean : infoCatBeans) {
                finalDb.save(infoCatBean);
            }
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_SAVE_ERROR_001, "InfoCatDaoImpl.saveInfoCats()" + "保存资讯类别信息失败！");
            return false;
        }
    }

    /**
     * @Description: 批量删除资讯类别
     * @param: @param idArray
     * @param: @return
     * @return: boolean
     * @throws
     * @author heliang
     * @Date 2013-9-9 上午7:19:15
     */
    public boolean deleteInfoCats(Integer[] idArray) throws DaoException{
        try {
            String strWhere = " INFOCATID IN ( ";
            for (int i = 0; i < idArray.length; i++) {
                if (i != (idArray.length - 1)) {
                    strWhere += idArray[i] + ",";
                } else {
                    strWhere += idArray[i] + ")";
                }
            }

            finalDb.deleteByWhere(InfoCatBean.class, strWhere);
            return true;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002, "InfoCatDaoImpl.deleteInfoCats()" + "根据资讯类别infoCatId删除资讯类别失败！");
            return false;
        }

    }

    /**
     * @Title: findAllInfoCats
     * @Description: 查询所有数据
     * @param: @return
     * @return: List<InfoCatBean>
     * @throws
     */
    public List<InfoCatBean> findAllInfoCats() throws DaoException{
        List<InfoCatBean> infoCatBeans = new ArrayList<InfoCatBean>();
        try {
            infoCatBeans = finalDb.findAllByWhere(InfoCatBean.class, "", " ORDER BY INFOCATORDER ");
            return infoCatBeans;
        } catch (DbException e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "InfoCatDaoImpl.findAllInfoCats()" + "查询所有资讯类别失败！");
            return null;
        }
    }
}
